package cn.itsource.service.impl;

import cn.itsource.AjaxResult;
import cn.itsource.domain.Product;
import cn.itsource.domain.Store;
import cn.itsource.dto.ProductDto;
import cn.itsource.mapper.ProductMapper;
import cn.itsource.service.ProductService;
import cn.itsource.feign.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private StoreService storeService;

    @Override
    public AjaxResult add(ProductDto productDto) {
        // 1.保存商品信息到数据库
        try {
            Product product = new Product();
            product.setName(productDto.getName());
            product.setPrice(productDto.getPrice());
            productMapper.add(product);
            // 2.保存库存到库存表
            Store store = new Store();
            store.setProduct_id(product.getId());
            store.setNumber(productDto.getNumber());
            AjaxResult add = storeService.add(store);
            if (!add.getSuccess()) {
                return add;
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
}
